package com.cxc.canal.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Map;

@Component
@Slf4j
public class DirectReceiver {

    /*@Autowired
    private RedisTemplate redisTemplate;
 
    @RabbitListener(queues = "exchange.trade.canal")//监听的队列名称 exchange.trade.canal
    public void process(Message message) throws UnsupportedEncodingException {
        String json  = new String(message.getBody());
        log.info("DirectReceiver消费者收到消息:{}",json);
        Map map = JSON.parseObject(json,Map.class);
        JSONArray array = null;
        String sqlType = (String) map.get("type");
        if(StringUtils.endsWithIgnoreCase("SELECT",sqlType)){
            array = JSONArray.parseArray((String)map.get("data"));
        }else{
            array = (JSONArray)map.get("data");
        }
        JSONObject object = array.getJSONObject(0);
        if(StringUtils.endsWithIgnoreCase("UPDATE",sqlType)
                || StringUtils.endsWithIgnoreCase("INSERT",sqlType)
                    || StringUtils.endsWithIgnoreCase("SELECT",sqlType)){
            redisTemplate.boundValueOps(object.get("id")).set(object.toString());
        }else if(StringUtils.endsWithIgnoreCase("DELETE",sqlType)){
            redisTemplate.delete(object.get("id"));
        }
    }*/
}